package com.heitian.ssm.controller;

import com.google.gson.JsonObject;
import com.heitian.ssm.model.Classify;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.service.ClassifyService;
import com.heitian.ssm.utils.CommonUtils;
import com.heitian.ssm.utils.GsonUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者： 陈建峰
 * 创建时间： 17/10/18 下午1:13
 * 版本： [1.0, 17/10/18]
 * 描述： 相册接口
 */
@Controller
@RequestMapping("/classify")
public class ClassifyController {

    private Logger log = Logger.getLogger(ClassifyController.class);

    @Resource
    ClassifyService classifyService;

    /**
     * 保存文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/saveClassify", method = RequestMethod.POST)
    public void saveClassify(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        Classify classify = new Classify();
        if (paras.has("classifyName")) {
            classify.setClassifyName(paras.get("classifyName").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("classifyHeadurl")) {
            classify.setClassifyHeadurl(paras.get("classifyHeadurl").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("classifyNote")) {
            classify.setClassifyNote(paras.get("classifyNote").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        classify.setClassifyGuid(CommonUtils.GetUUID());
        classify.setCreatTime(CommonUtils.getCurrentTime());
        classifyService.saveClassify(classify);
        response.getWriter().write(CommonUtils.createJson("1", "添加成功！", null).toString());
    }

    /**
     * 删除文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/deleteClassify", method = RequestMethod.POST)
    public void deleteClassify(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String classifyGuids = paras.get("classifyGuids").getAsString();
        String[] classifyGuid = classifyGuids.split(";");
        for (int i = 0; i < classifyGuid.length; i++) {
            classifyService.deleteClassify(classifyGuid[i]);
        }
        response.getWriter().write(CommonUtils.createJson("1", "删除成功！", null).toString());
    }


    /**
     * 分页获取相册数据
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllclassify", method = RequestMethod.POST)
    public void getAllclassify(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String index = paras.get("index").getAsString();
        String currentPageCount = paras.get("currentPageCount").getAsString();
        PageBean<Classify> pageBean = classifyService.getAllclassify(Integer.parseInt(index),Integer.parseInt(currentPageCount));
        String UserArea = null;
        String code = "0";
        String message = "";
        if (pageBean != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(pageBean);
        } else {
            message = "用户数据获取失败！";
        }
        response.getWriter().write(CommonUtils.createJson(code, message, UserArea).toString());
    }

    /**
     * 获取相册所有数量
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllClassifyCount", method = RequestMethod.POST)
    public void getAllClassifyCount(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        int count = classifyService.getAllClassifyCount();
        Map<String,String> map = new HashMap<String, String>();
        map.put("count",count+"");
        String UserArea = GsonUtil.GsonString(map);
        response.getWriter().write(CommonUtils.createJson("1", "", UserArea).toString());
    }

}
